package com.quanfaqiang.demo1.controller;


import com.quanfaqiang.demo1.pojo.Result;
import com.quanfaqiang.demo1.utils.AliOssUtil;
import jakarta.servlet.MultipartConfigElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(DataSize.ofMegabytes(10)); // 设置最大文件大小为10MB
        factory.setMaxRequestSize(DataSize.ofMegabytes(20)); // 设置最大请求大小
        return factory.createMultipartConfig();
    }

    //    @PostMapping("upload")
//    public Result<String> upload(MultipartFile file) throws Exception
//    {
//        String originalFilename = file.getOriginalFilename();
//        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        //file.transferTo(new File("C:\\Users\\QFQ\\Desktop\\big_event\\file\\"+filename));
//        String url= AliOssUtil.uploadFile(filename, file.getInputStream());
//        return Result.success(url);
//    }
    @Autowired
    private Environment env;
//
////    @PostMapping("/upload")//方法一
//    public Result imgUpDown(MultipartFile file, HttpServletRequest request) throws IOException {
//        System.out.println("上传文件运行了");
//        if (!file.isEmpty()) {
//
//            String fileName = file.getOriginalFilename();
//
//            System.out.println(fileName+"===========");
//            String suffixName = fileName.substring(fileName.indexOf("."));
//            System.out.println(suffixName);
//            //设置上传文件的保存地址目录
//            String dirpath = request.getServletContext().getRealPath("/upload");
//            System.out.println(dirpath);
//            File parentFilePath = new File(dirpath);
//            //如果保存文件不存在就先创建目录
//            if (!parentFilePath.exists()) {
//                parentFilePath.mkdir();
//            }
//            String fileNewName = UUID.randomUUID() + fileName;
//
//            File newFile = new File(parentFilePath, fileNewName);
//            file.transferTo(newFile);
//            System.out.println(newFile);
//            return Result.success(newFile);
//        }
//        return null;
//    }
//
//
////方法二
    @PostMapping("/download")
    public Result saveFile(MultipartFile file) {
        System.out.println("上传文件运行了");

        if (file.isEmpty()) {
            return Result.success("文件为空！");
        }
        // 给文件重命名
        String fileName = UUID.randomUUID() + "." + file.getContentType()
                .substring(file.getContentType().lastIndexOf("/") + 1);
        System.out.println(fileName+"===============");
        try {
            // 获取保存路径
            String path = getSavePath();
            File files = new File(path, fileName);
            System.out.println(files);
            File parentFile = files.getParentFile();
            System.out.println(parentFile);
            if (!parentFile.exists()) {
                parentFile.mkdir();
            }
            file.transferTo(files);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return Result.success(fileName); // 返回重命名后的文件名
//        return Result.success("http://localhost:9095/upload/"+fileName); // 返回重命名后的文件名
        System.out.println(getPort());
        System.out.println("http://localhost:"+getPort()+"/upload/"+fileName);
        return Result.success("http://localhost:"+getPort()+"/upload/"+fileName); // 返回重命名后的文件名
    }

    public String getSavePath() {
        // 这里需要注意的是ApplicationHome是属于SpringBoot的类
        // 获取项目下resources/static/img路径
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//
//        // 保存目录位置根据项目需求可随意更改
        return applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() + "\\upload";
//                .getParentFile().getAbsolutePath() + "\\src\\main\\resources\\upload";

    }
    public int getPort() {
        // 获取当前应用绑定的端口号
        return env.getProperty("local.server.port", Integer.class);
    }
@PostMapping("upload")
public Result<String> upload(MultipartFile file) throws Exception
{
    String originalFilename = file.getOriginalFilename();
    String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
    //file.transferTo(new File("C:\\Users\\QFQ\\Desktop\\big_event\\file\\"+filename));
    String url= AliOssUtil.uploadFile(filename, file.getInputStream());
    return Result.success(url);
}

}

